#!/bin/bash
# Install dependencies for Muto ROS2 Controller

set -e  # Exit on any error

echo "=== Installing Dependencies for Muto ROS2 Controller ==="

# Check if we're in a ROS2 environment
if [ -z "$ROS_DISTRO" ]; then
    echo "Warning: ROS_DISTRO not set. Please source ROS2 setup first:"
    echo "  source /opt/ros/foxy/setup.bash"
fi

# Update system packages
echo "Updating system packages..."
sudo apt update

# Install system dependencies
echo "Installing system dependencies..."
sudo apt install -y python3-pip python3-dev python3-setuptools python3-wheel

# Update pip
echo "Updating pip..."
python3 -m pip install --upgrade pip

# Install Python dependencies with specific versions for stability
echo "Installing Python dependencies..."
pip3 install --user fastapi==0.104.1 uvicorn[standard]==0.24.0 pydantic==2.5.0 requests==2.31.0

# Install additional ROS2 Python dependencies
echo "Installing ROS2 Python dependencies..."
pip3 install --user setuptools wheel

# Verify installations
echo "Verifying installations..."
python3 -c "import fastapi, uvicorn, pydantic, requests; print('All Python dependencies installed successfully!')"

echo "=== Dependencies installation completed! ==="
echo ""
echo "Next steps:"
echo "1. Build the package:"
echo "   cd ~/muto-llm/muto_llm_ws"
echo "   colcon build --packages-select muto_ros2_controller"
echo ""
echo "2. Source the workspace:"
echo "   source install/setup.bash"
echo ""
echo "3. Run the node:"
echo "   ros2 launch muto_ros2_controller muto_controller.launch.py"
echo ""
echo "If you encounter permission issues, you may need to run:"
echo "   sudo chown -R $USER:$USER ~/.local"